<template>
  <div class="task-detail">
    <el-row>
      <el-col :span="24">
        <div class="header">
          <el-button type="primary" @click="goBack">
              <el-icon><CloseBold /></el-icon> 返回
          </el-button>
          <el-button type="success" @click="submitReport">
              <el-icon><Select /></el-icon> 提交
          </el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <h1 class="task-name">{{ task.name }}</h1>
        <div class="task-meta">
          <span class="task-assignee">{{ task.assignee }}</span>
          <span class="task-time">
            <i class="el-icon-time"></i>
            {{ formatDate(task.startTime) }} - {{ formatDate(task.endTime) }}
          </span>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="22">
        <div class="task-description">
          <p>{{ task.taskDescription }}</p>
        </div>
      </el-col>
      <el-col :span="2">
        <el-upload
          action="#"
          list-type="text"
          class="upload-demo"
        >
          <el-button type="primary"><el-icon><Upload /></el-icon>上传文件</el-button>
        </el-upload>
      </el-col>
      <el-input
        type="textarea"
        v-model="report"
        placeholder="填写任务完成报告"
        rows="10"
        class="report-input"
      ></el-input>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const task = JSON.parse(route.query.task)
const report = ref(task.taskResponse || '')  // 如果有 taskResponse，设置为其值，否则为空字符串

const formatDate = (dateStr) => {
  const date = new Date(dateStr)
  return date.toLocaleDateString()
}

const goBack = () => {
  router.back()
}

const submitReport = () => {
  // 这里可以添加提交报告的逻辑
  console.log(task)
  console.log('Report submitted:', report.value)
  router.push({ name: 'memeber-project', params: { id: task.projectId } })
}
</script>

<style scoped>
.task-detail {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

p {
  text-align: left;
  max-height: 90px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.task-name {
  text-align: left;
  margin: 0;
  font-size: 2em;
  color: #333;
}

.task-meta {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  margin-bottom: 20px;
  font-size: 0.9em;
}

.task-assignee {
  color: gray;
}

.task-time {
  color: goldenrod;
  display: flex;
  align-items: center;
}

.task-time i {
  margin-right: 5px;
}

.task-description {
  margin-bottom: 20px;
}

.report-input {
  margin-bottom: 20px;
}
</style>
